package com.itheima.crm.controller;



import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.servlet.ModelAndView;

import com.itheima.common.utils.Page;

import com.itheima.crm.pojo.RiZhi;

import com.itheima.crm.pojo.gonggao;



import com.itheima.crm.service.RiZhiService;

import com.itheima.crm.service.gonggaoService;


/**
 * 客户管理
 * 
 * @author lx
 *
 */
@Controller
public class GongGaoController {
	@Autowired
	private gonggaoService gonggaoService;
	
	@Autowired
	private RiZhiService rizhiService;
	@Autowired
	public gonggaoService getGonggaoService() {
		return gonggaoService;
	}



	public void setGonggaoService(gonggaoService gonggaoService) {
		this.gonggaoService = gonggaoService;
	}



	public RiZhiService getRizhiService() {
		return rizhiService;
	}



	public void setRizhiService(RiZhiService rizhiService) {
		this.rizhiService = rizhiService;
	}



	//员工看的内容
	@RequestMapping(value = "/GongGao")
	public ModelAndView list(HttpServletRequest request, gonggao vo, Model model,String fromtime,String totime) {
		
		
		String fromdate="";
		String todate="";
		
		if(fromtime!=null||totime!=null) {
		String year=fromtime.substring(fromtime.length()-4);
		
		String mouth=fromtime.substring(0,2);
		String day=fromtime.substring(3,5);
		
		 fromdate=year+"-"+mouth+"-"+day;
		year=fromtime.substring(totime.length()-4);
		
		 mouth=totime.substring(0,2);
		 day=totime.substring(3,5);
		 todate=year+"-"+mouth+"-"+day;
		 System.out.println(fromdate+"==========="+todate);
		 request.getSession().setAttribute("from", fromdate);
		 request.getSession().setAttribute("to", todate);
		}
		HttpSession session=request.getSession(false);
		String zhiwei = (String) session.getAttribute("zhiwei");
		
		Page<gonggao> page=null;
		if (zhiwei.equals("1")) {
			ModelAndView a=new ModelAndView();
			
			page = gonggaoService.selectgonggao(fromdate,todate,vo);
			a.addObject("page",page);
			a.addObject("from", fromdate);
			a.addObject("to", todate);
			a.setViewName("gonggao");
			
			return a;
		}else {
	
			ModelAndView a=new ModelAndView();
	
		 page = gonggaoService.selectgonggaoAll(vo);
		
		a.addObject("page",page);
		a.setViewName("gonggao");
		return a;}
	}
	
	
	
	//新增公文页面
	@RequestMapping(value = "/GongGaoNew")
	public ModelAndView newlist(HttpServletRequest request, gonggao vo, Model model) {
		HttpSession session=request.getSession(false);
		String zhiwei = (String) session.getAttribute("zhiwei");
		
		if (!zhiwei.equals("1")) {
			ModelAndView a=new ModelAndView();
			a.setViewName("GongGaoNew");
			
			gonggao gonggao=new gonggao();
			a.addObject("gonggao", gonggao);
			return a;
		}else {
	
			ModelAndView a=new ModelAndView();
	
			a.setViewName("error");

		     return a;}
	
		
		
		
	
		
		
		
		
		
		
		
		
	}
	
	//新增公文功能
	@RequestMapping(value = "/newGongGao")
	public String newGongGao(gonggao vo,HttpServletRequest request) {
		gonggaoService.insertgonggao(vo);
		System.out.println("插入");
		
		RiZhi a=new RiZhi();
		request.getSession().getAttribute("ygid");
		a.setYgid((Integer)request.getSession().getAttribute("ygid"));
		a.setYgname((String)request.getSession().getAttribute("ygname"));
		a.setInsertdate(new Date());
		System.out.println(a.getInsertdate());
		a.setDetial("新建公告");
		rizhiService.insertApplication(a);
		return "redirect:/GongGaoNew";
	}
	@RequestMapping(value = "/GongGaochange")
	public String GongGaochange(String ifshow,Integer id,HttpServletRequest request) {
		gonggaoService.changeShow(ifshow, id);
		System.out.println("修改显示");
		
		RiZhi a=new RiZhi();
		request.getSession().getAttribute("ygid");
		a.setYgid((Integer)request.getSession().getAttribute("ygid"));
		a.setYgname((String)request.getSession().getAttribute("ygname"));
		a.setInsertdate(new Date());
		System.out.println(a.getInsertdate());
		a.setDetial("修改公告状态");
		rizhiService.insertApplication(a);
		return "redirect:/GongGao";
	}

	
	
	
	@InitBinder
	protected void init(HttpServletRequest request, ServletRequestDataBinder binder) {
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		dateFormat.setLenient(false);
		binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false));
	}
	
	

}
